//
//  LiveCoursewareResultView.m
//  LivePlayer
//
//  Created by liang on 2019/3/5.
//

#import "LiveCoursewareResultView.h"
#import "Masonry.h"
#import "UIView+WXAutoLayout.h"

@implementation LiveCoursewareResultView

- (instancetype)init {
    
    if (self = [super init]) {
        self.backImageView = [[UIImageView alloc] init];
        self.backImageView.userInteractionEnabled = YES;
        
        self.goldLabel = [[UILabel alloc] init];
        self.goldLabel.textColor = [UIColor colorWithHexValue:0xEFE30F];
        self.goldLabel.font = [UIFont boldSystemFontOfSize:20];
        self.goldLabel.adjustsFontSizeToFitWidth = YES;
        
        self.tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
        self.tableView.backgroundColor = [UIColor clearColor];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        self.tableView.separatorColor = [UIColor colorWithHexValue:0xA4753F];
        self.tableView.separatorInset = UIEdgeInsetsMake(0, 30, 0, 30);
        self.tableView.tableFooterView = [UIView new];
        
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"LiveCoursewareResultViewCell"];
        self.closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.closeButton addTarget:self action:@selector(closeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
        
        
//        [self addSubview:self.backImageView];
        [self.backImageView addSubview:self.tableView];
        [self addSubview:self.closeButton];
        
        self.isShowPanel = YES;
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    
    UIView *maskView = [[UIView alloc] initWithFrame:self.bounds];
    maskView.backgroundColor = [UIColor blackColor];
    maskView.alpha = 0.5;
    [self addSubview:maskView];
    [self sendSubviewToBack:maskView];
    
    NSString *preString = self.isShowPanel?@"interact-active-":@"game-";
    NSString *jsonName = [NSString stringWithFormat:@"%@%d",preString,self.type];
    
    self.boyAnimationView = [LOTAnimationView animationNamed:jsonName inBundle:[NSBundle mainBundle]];
    
    CGFloat scaleX = IS_IPHONE_X ? 0.85 : 1;
    CGFloat scaleXright = IS_IPHONE_X ? 20 : 0;
    float boyWidth = self.width; //* 0.3;
    self.boyAnimationView.frame = CGRectMake(0, 0, boyWidth *scaleX, boyWidth * 0.56 *scaleX);
    self.boyAnimationView.centerY = self.height / 2 * 0.92;
    self.boyAnimationView.centerX = self.width / 2;
    self.boyAnimationView.userInteractionEnabled = NO;
    self.boyAnimationView.loopAnimation = YES;
    self.boyAnimationView.contentMode = UIViewContentModeScaleAspectFit;
    self.boyAnimationView.animationSpeed = 0.5;
    [self addSubview:self.boyAnimationView];
    [self.boyAnimationView play];
    
    [self.boyAnimationView addSubview:self.goldLabel];
    
    [self.boyAnimationView insertSubview:self.backImageView atIndex:0];
    
    self.goldLabel.text = [NSString stringWithFormat:@"+%@",self.glod];
//    [self bringSubviewToFront:self.goldLabel];
    
    //button
    //self.closeButton.size = CGSizeMake(44, 44);
    [self.closeButton setImage:[UIImage imageNamed:@"live_interact_primary_close"] forState:UIControlStateNormal];
    
    if (self.isShowPanel) {
        
        self.backImageView.hidden = NO;
        self.tableView.hidden = NO;
        UIImage *backImage = [UIImage imageNamed:@"live_interact_primary_bg"];
        float widthOfBackImageV = self.width * 0.7 *scaleX;
        float heightOfBackImageV = widthOfBackImageV * backImage.size.height / backImage.size.width;
        self.backImageView.frame = CGRectMake(0, 0, widthOfBackImageV, heightOfBackImageV);
        self.backImageView.image = backImage;
        self.backImageView.centerX = self.boyAnimationView.width / 2;
        self.backImageView.centerY = self.boyAnimationView.height / 2 * 1.3;
        
        self.goldLabel.size = CGSizeMake(40, 40);
        self.goldLabel.centerX = self.backImageView.centerX * 1.21;
        self.goldLabel.centerY = self.boyAnimationView.height * 0.37;
        
//        self.closeButton.bottom = self.height * 0.2;
//        self.closeButton.right = CGRectGetMaxX(self.backImageView.frame);
        
        CGRect frame = [self.backImageView.superview convertRect:self.backImageView.frame toView:self];
        [self.backImageView removeFromSuperview];
        self.backImageView.frame = frame;
        [self insertSubview:self.backImageView atIndex:1];
        
        self.closeButton.frame =  CGRectMake(CGRectGetMaxX(frame)-44, frame.origin.y-20-44, 44, 44);
        
        //tableView
        self.tableView.size = CGSizeMake(self.backImageView.width * 0.92, self.backImageView.height * 0.55);
        self.tableView.backgroundColor = [UIColor clearColor];
        self.tableView.centerX = self.backImageView.width / 2;
        self.tableView.centerY = self.backImageView.height / 2 * 1.2;
        [self.tableView reloadData];
        
    }else {
        
        self.closeButton.hidden = YES;
        self.backImageView.hidden = YES;
        self.tableView.hidden = YES;
        
        self.boyAnimationView.centerY = self.height / 2;
        
        self.goldLabel.font = [UIFont boldSystemFontOfSize:25];
        self.goldLabel.size = CGSizeMake(40, 40);
        self.goldLabel.centerX = self.boyAnimationView.width / 2 * 1.07;
        self.goldLabel.centerY = self.boyAnimationView.height / 2 * 1.58;
      
        self.closeButton.centerX = self.boyAnimationView.centerX * 1.45;
        self.closeButton.centerY = self.boyAnimationView.centerY * 0.5;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (self.resultCloseBlock) {
                self.resultCloseBlock();
            }
        });
    }
}

- (void)closeButtonPressed:(id)sender {
    
    if (self.resultCloseBlock) {
        self.resultCloseBlock();
    }
}

#pragma mark --   UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"LiveCoursewareResultViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"---%ld",indexPath.row];
    return cell;
}

#pragma mark --   UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}

@end
